﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hsinchu.BLL;
using Hsinchu.Model;

public partial class Admin_ProductMgr_ItemShow : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.drpDisable.SelectedItem.Text == "出售中")
            {
                this.btnDisable.Text = "停售";
            }
            else
            {
                this.btnDisable.Text = "出售中";
            }
            InitLoad();
        }
    }

    void InitLoad()
    {
        BindCategoryCombox();
        BindItemGridView(0);
    }

    /// <summary>
    /// 绑定Item列表
    /// </summary>
    void BindItemGridView(int isdisable)
    {
        ItemMgr dllItemMgr = new ItemMgr();
        this.ItemGridView.DataSource = dllItemMgr.GetItemByIsDisable(isdisable);
        this.ItemGridView.DataBind();
    }


    /// <summary>
    /// 绑定类别dropdownList
    /// </summary>
    void BindCategoryCombox()
    {
        CategoryMgr dllCategoryMgr = new CategoryMgr();
        Dictionary<string, string> category = dllCategoryMgr.GetCategoryByParentCategoryId(0);
        foreach (KeyValuePair<string, string> item in category)
        {
            ListItem _li = new ListItem();
            _li.Text = item.Key;
            _li.Value = item.Value;
            this.drpCategory.Items.Add(_li);
        }
    }


    /// <summary>
    /// 添加新产品
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        MItem item = new MItem();
        CategoryMgr dllCategoryMgr = new CategoryMgr();
        item.Name = this.txtItemName.Text;
        item.CategoryId = Convert.ToInt32(this.drpCategory.SelectedValue);
        item.TableName = this.txtTableName.Text;
        ItemMgr dllItemMgr = new ItemMgr();
        dllItemMgr.AddItem(item);
    }

    /// <summary>
    /// 停止销售按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisable_Click(object sender, EventArgs e)
    {
        List<int> itemid = new List<int>();
        for (int i = 0; i < this.ItemGridView.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.ItemGridView.Rows[i].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                itemid.Add(Convert.ToInt32(this.ItemGridView.Rows[i].Cells[1].Text));
            }
        }
        if (this.drpDisable.SelectedItem.Text == "出售中")
        {
            ItemMgr dllItemMgr = new ItemMgr();
            dllItemMgr.DisableItem(itemid);
            BindItemGridView(0);
        }
        else
        {
            ItemMgr dllItemMgr = new ItemMgr();
            dllItemMgr.UnDisableItem(itemid);
            BindItemGridView(1);
        }
        

    }


    /// <summary>
    /// 删除产品按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        List<int> itemid = new List<int>();
        for (int i = 0; i < this.ItemGridView.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.ItemGridView.Rows[i].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                itemid.Add(Convert.ToInt32(this.ItemGridView.Rows[i].Cells[1].Text));
            }
        }
        ItemMgr dllItemMgr = new ItemMgr();
        dllItemMgr.DeleteItem(itemid);
    }

    /// <summary>
    /// 产品状态切换事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>    
    protected void drpDisable_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (this.drpDisable.SelectedItem.Text == "出售中")
        {
            BindItemGridView(0);
        }
        else
        {
            BindItemGridView(1);
        }
        if (this.drpDisable.SelectedItem.Text == "出售中")
        {
            this.btnDisable.Text = "停售";
        }
        else
        {
            this.btnDisable.Text = "出售中";
        }
    }
}
